package com.rocket.excel;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.rocket.entity.FirearmType;
import com.rocket.entity.Work;

public class WorkExcelUtil extends ExcelUtil {

	private static WorkExcelUtil	mInstance;

	public static synchronized WorkExcelUtil getInstance() {
		if (mInstance == null) {
			mInstance = new WorkExcelUtil();
		}
		return mInstance;
	}

	public void export(final ArrayList<Work> works, final String path) {
		if (works == null || works.size() == 0) {
			return;
		}
		new Thread() {

			public void run() {

				File file = new File(path);

				try {
					WritableWorkbook wwb = Workbook.createWorkbook(file);
					WritableSheet sheet = wwb.createSheet("作业", 0);
					sheet.addCell(new Label(0, 0, "作业火器类型"));
					sheet.addCell(new Label(1, 0, "作业点站名"));
					sheet.addCell(new Label(2, 0, "作业点编号"));
					sheet.addCell(new Label(3, 0, "固定作业点经纬度"));
					sheet.addCell(new Label(4, 0, "流动作业点经纬度"));
					sheet.addCell(new Label(5, 0, "作业点海拔高度（米）"));
					sheet.addCell(new Label(6, 0, "作业云系类型"));
					sheet.addCell(new Label(7, 0, "作业云顶高度（米）"));
					sheet.addCell(new Label(8, 0, "作业云底高度（米）"));
					sheet.addCell(new Label(9, 0, "作业云系强度中心高度（米）"));
					sheet.addCell(new Label(10, 0, "作业云层-6°C高度（米）"));
					sheet.addCell(new Label(11, 0, "火箭发射仰角（度）"));
					sheet.addCell(new Label(12, 0, "火箭最大射高（米）"));
					sheet.addCell(new Label(13, 0, "火箭最大射程（米）"));
					sheet.addCell(new Label(14, 0, "高炮发射仰角（度）"));
					sheet.addCell(new Label(15, 0, "炮弹引信自爆时间（秒）"));
					sheet.addCell(new Label(16, 0, "炮弹最大射高（米）"));
					sheet.addCell(new Label(17, 0, "炮弹最大射程（米）"));
					sheet.addCell(new Label(18, 0, "创建时间"));

					for (int i = 0; i < works.size(); i++) {
						Work work = works.get(i);
						String type;
						switch (work.firearmType) {
							case FirearmType.ROCKET_BL1:
								type = "BL-1型火箭弹";
								break;
							case FirearmType.ROCKET_HJD82:
								type = "HJD-82型火箭弹";
								break;
							case FirearmType.ROCKET_RY6300:
								type = "RYI-6300型火箭弹";
								break;
							case FirearmType.ROCKET_WR98:
								type = "WR-98型火箭弹";
								break;
							case FirearmType.MISSILE_83:
								type = "83型炮弹";
								break;
							case FirearmType.MISSILE_JD89:
								type = "JD-89型炮弹";
								break;
							default:
								type = "";
								break;
						}
						sheet.addCell(new Label(0, i + 1, type));
						sheet.addCell(new Label(1, i + 1, work.name));
						sheet.addCell(new Label(2, i + 1, work.number));
						sheet.addCell(new Label(3, i + 1, work.fixedLongitude + "/" + work.fixedLatitude));
						sheet.addCell(new Label(4, i + 1, work.moveLongitude + "/" + work.moveLatitude));
						sheet.addCell(new Label(5, i + 1, String.valueOf(work.haiba)));
						sheet.addCell(new Label(6, i + 1, work.cloudType));
						sheet.addCell(new Label(7, i + 1, String.valueOf(work.cloudTopHeight)));
						sheet.addCell(new Label(8, i + 1, String.valueOf(work.cloudBottomHeight)));
						sheet.addCell(new Label(9, i + 1, String.valueOf(work.cloudCenterHeight)));
						sheet.addCell(new Label(10, i + 1, String.valueOf(work.cloud6Height)));
						if (work.firearmType < 10) {
							sheet.addCell(new Label(11, i + 1, String.valueOf(work.rocketAngle)));
							sheet.addCell(new Label(12, i + 1, String.valueOf(work.rocketMaxHeight)));
							sheet.addCell(new Label(13, i + 1, String.valueOf(work.rocketMaxLength)));
							sheet.addCell(new Label(14, i + 1, ""));
							sheet.addCell(new Label(15, i + 1, ""));
							sheet.addCell(new Label(16, i + 1, ""));
							sheet.addCell(new Label(17, i + 1, ""));
						} else {
							sheet.addCell(new Label(11, i + 1, ""));
							sheet.addCell(new Label(12, i + 1, ""));
							sheet.addCell(new Label(13, i + 1, ""));
							sheet.addCell(new Label(14, i + 1, String.valueOf(work.missileAngle)));
							sheet.addCell(new Label(15, i + 1, String.valueOf(work.misseleTime)));
							sheet.addCell(new Label(16, i + 1, String.valueOf(work.missileMaxHeight)));
							sheet.addCell(new Label(17, i + 1, String.valueOf(work.missileMaxLength)));
						}
						SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
						sheet.addCell(new Label(18, i + 1, format.format(new Date(work.createTime))));

					}

					// //作用是从(m,n)到(p,q)的单元格全部合并，比如：
					// WritableSheet sheet=wwb.createSheet("第一页",0);
					//
					// //合并第一列第一行到第六列第一行的所有单元格
					// sheet.mergeCells(0,0,5,0);

					wwb.write();
					wwb.close();
				} catch (Exception e) {
					e.printStackTrace();
				}

			};
		}.start();
	}
}
